<?php
session_start();
include_once "../apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$foodName = $_POST["food_name"];
$foodDetail = $_POST["food_detail"];
$price = $_POST["price"];
$petWeight = $_POST["pet_weight"];
$speciesId = $_POST["species_id"];

$foodId = $_GET["food_id"];
if($foodId == "" || $foodId == null){
	$foodId = $_POST["food_id"];
}

$mode = $_GET["mode"];
if($mode == "" || $mode == null){
	$mode = $_POST["mode"];
}

$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}

if($mode == "insert"){
	$arrData["food_name"] = "".$foodName."";
	$arrData["food_detail"] ="".$foodDetail."";
	$arrData["price"] ="".$price."";
	$arrData["pet_weight"] ="".$petWeight."";
	$arrData["species_id"] ="".$speciesId."";
	$arrData["create_date"] = "".date("Y-m-d")."";
	$arrData["create_by"] = "".$_SESSION["user_name"]."";
	$managerIUDS->inserData($arrData, "food");
	header("Location:../contents/food_main.php");
}else if($mode == "delete"){
	$arrPrimaryKey["food_id"] = "".$foodId."";
	$managerIUDS->deleteData("food", $arrPrimaryKey, "");
	header("Location:../contents/food_main.php");
}else if($mode == "update"){
	$arrData["food_name"] = "".$foodName."";
	$arrData["food_detail"] ="".$foodDetail."";
	$arrData["price"] ="".$price."";
	$arrData["pet_weight"] ="".$petWeight."";
	$arrData["species_id"] ="".$speciesId."";
	$arrData["update_date"] = "".date("Y-m-d")."";
	$arrData["update_by"] = "".$_SESSION["user_name"]."";
	$arrPrimaryKey["food_id"] = "".$foodId."";
	$managerIUDS->updateData($arrData, "food", $arrPrimaryKey, "");
	header("Location:../contents/food_main.php");
}else if($mode == "search"){
	$foodName = $_GET["food_name"];
	header("Location:../contents/food_main.php?mode=search&page=".$page."&food_name=".urlencode($foodName));
}else if($mode == "listBreedData"){
	$foodData = array();
	$foodName = $_GET["food_name"];
	if($foodName == "" || $foodName == null){
		$foodName = $_POST["food_name"];
	}
	$foodData = $managerIUDS->queryData("food_name", "food", " WHERE food_name LIKE '%".$foodName."%'", "2", "");
	echo json_encode($foodData);
}
?>